﻿#include <iostream>
#include <string>
#include "tcpclient.h"
#include "frame_protocol.h"
#include "debug.cpp"

#define CMD_BUSINESS_CMD_USERAUTH_START 0x00

using namespace std;
using namespace uv;

std::string serverip;
int call_time = 0;
bool is_exist = false;

void CloseCB(int clientid, void* userdata)
{
    fprintf(stdout, "cliend close\n");
    TCPClient* client = (TCPClient*)userdata;
    client->Close();
}

void ReadCB(const NetPacket& packet, const unsigned char* buf, void* userdata)
{
#if 0
	//LOGD("packet direction:%d, type:%d, action:%d, datalen :%d", packet.direction, packet.type, packet.action, packet.datalen);
    char senddata[256] = {0};
    TCPClient* client = (TCPClient*)userdata;

    sprintf(senddata, "****recv server data(%p,%d)", client, packet.datalen);
    fprintf(stdout, "%s\n", senddata);
    NetPacket tmppack = packet;
	tmppack.cmd = CMD_TEST_4;
    //tmppack.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
    StreamBuffer retstr = PacketData(tmppack, (const unsigned char*)senddata,strlen(senddata));
    if (client->Send(&retstr[0], retstr.length()) <= 0) {
        fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
    }
    fprintf(stdout, "call time %d\n", ++call_time);
    //if (call_time > 50) {
    //    is_exist = true;
    //}
#endif
}
void ReConnectCB(NET_EVENT_TYPE eventtype, void* userdata)
{
#if 0
    TCPClient* client = (TCPClient*)userdata;
    if (NET_EVENT_TYPE_RECONNECT == eventtype) {
        fprintf(stdout, "succeed reconnect.\n");
        char senddata[256];
        memset(senddata, 0, sizeof(senddata));
        sprintf(senddata, "client(%p) call %d", client, ++call_time);
        NetPacket packet;
        packet.version = NET_PACKAGE_VERSION;
        //packet.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
        StreamBuffer str = PacketData(packet, (const unsigned char*)senddata,strlen(senddata));
        if (client->Send(&str[0], str.length()) <= 0) {
            fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
        } else {
            fprintf(stdout, "send succeed:%s\n", senddata);
        }
    } else {
        fprintf(stdout, "server disconnect.\n");
    }
#endif
}

int main(int argc, char** argv)
{
    if (argc != 2) {
        fprintf(stdout, "usage: %s server_ip_address\neg.%s 192.168.1.1\n", argv[0], argv[0]);
        return 0;
    }
    //DeclareDumpFile();
    serverip = argv[1];

    //const int clientsize = std::stoi(argv[2]);
    //const int clientsize = atoi(argv[2]);
    TCPClient pClients;
    TCPClient::StartLog("log/");

    int i = 0;
    char senddata[256];
    pClients.SetRecvCB(ReadCB, &pClients);
    pClients.SetClosedCB(CloseCB, &pClients);
    pClients.SetReconnectCB(ReConnectCB, &pClients);
    if (!pClients.Connect(serverip.c_str(), 12345)) {
        fprintf(stdout, "connect error:%s\n", pClients.GetLastErrMsg());
    } else {
        fprintf(stdout, "client(%p) connect succeed.\n", &pClients);
    }

	{
		NetPacket packet;
		//packet.direction = FRA_DIR_APP_TO_SERVER;
        packet.version = NET_PACKAGE_VERSION;
		packet.cmd = CMD_REGISTER_APP;

        #pragma pack(1)//将当前字节对齐值设为1
        typedef struct _RegisterApp {
            unsigned char type;
            unsigned char username[20];
            unsigned char password[20];
        }RegisterApp;
        #pragma pack()//将当前字节对齐值设为默认值(通常是4)
        //packet.datalen = sizeof(RegisterApp);
        RegisterApp _register_app;
        _register_app.type = CMD_BUSINESS_CMD_USERAUTH_START;
        strcpy((char*)_register_app.username,"zhangli");
        strcpy((char*)_register_app.password,"1234567");
		StreamBuffer str = PacketData(packet, (const unsigned char*)&_register_app,sizeof(RegisterApp));
		if (pClients.Send(&str[0], str.length()) <= 0) {
			fprintf(stdout, "(%p)send error.%s\n", &pClients, pClients.GetLastErrMsg());
		} else {
			fprintf(stdout, "send succeed\n");
		}
	}

#if 0
	{
        memset(senddata, 0, sizeof(senddata));

        sprintf(senddata, "client(%p) call %d", &pClients, ++call_time);
        LOGD("send: %s, len:%d", senddata, strlen(senddata));
		NetPacket packet;
        packet.version = NET_PACKAGE_VERSION;
		//packet.direction = FRA_DIR_APP_TO_SERVER;
		packet.cmd = CMD_TEST_3;
		//packet.action = FRA_ACTION_USR_AUTH_SIGN;
		char* buffer = senddata;
		buffer+= StringToBuffer(StreamBuffer((const unsigned char*)"username"),(unsigned char*)buffer,256u);
		buffer+= StringToBuffer(StreamBuffer((const unsigned char*)"password"),(unsigned char*)buffer,256u - (buffer-senddata));
		
		//packet.datalen = buffer - senddata;
		StreamBuffer str = PacketData(packet, (const unsigned char*)senddata,buffer - senddata);
		if (pClients.Send(&str[0], str.length()) <= 0) {
			fprintf(stdout, "(%p)send error.%s\n", &pClients, pClients.GetLastErrMsg());
		} else {
			fprintf(stdout, "send succeed:%s\n", senddata);
		}
	}
#endif
	
    while (!is_exist) {
        sleep(10);
    }
    return 0;
}
